﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Box.Entities;
namespace Box.Repository
{
    public class InsumoRepository : MasterRepository, IInsumoRepository
    {

        public List<Insumo> GetAllFromInsumos()
        {
            var query = from c in Context.Insumos
                        orderby c.NombreInsumo+c.DescripcionInsumo ascending
                        select c;
            return query.ToList();

        }

        public List<Insumo> GetFromInsumosByCriterio(string criterio)
        {
            var query = from c in Context.Insumos.Include("Marca")
                        orderby c.NombreInsumo + c.DescripcionInsumo ascending
                        select c;
            if (criterio != null && criterio != string.Empty)
            {
                query = from c in query
                        where c.NombreInsumo.ToUpper().Contains(criterio.ToUpper()) ||
                              c.DescripcionInsumo.ToUpper().Contains(criterio.ToUpper())
                        orderby c.NombreInsumo + c.DescripcionInsumo ascending
                        select c;

            }
            return query.ToList();
        }

        public Insumo GetFromInsumosById(int id)
        {
            return Context.Insumos.Find(id);
        }

        public void AddInsumo(Insumo Insumo)
        {
            Context.Insumos.Add(Insumo);
            Context.SaveChanges();
        }

        public void UpdateInsumo(Insumo Insumo)
        {
            Context.Entry(Insumo).State = EntityState.Modified;
            Context.SaveChanges();
        }

        public void RemoveInsumo(int id)
        {
            var existe = Context.Insumos.Find(id);
            if (existe != null)
            {
                Context.Insumos.Remove(existe);
                Context.SaveChanges();
            }
        }

        public bool ExisteInsumo(int id)
        {
            var query = Context.Insumos.Where(p => p.Id.Equals(id));

            return query.Any();
        }
    }
}
